package com.wang.springbootinit.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.springbootinit.common.ErrorCode;
import com.wang.springbootinit.exception.BusinessException;
import com.wang.springbootinit.mapper.FeedbackMapper;
import com.wang.springbootinit.model.entity.Feedback;
import com.wang.springbootinit.model.entity.Student;
import com.wang.springbootinit.model.entity.User;
import com.wang.springbootinit.service.FeedbackService;
import com.wang.springbootinit.service.StudentService;
import com.wang.springbootinit.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 信息反馈服务实现类
 * 提供学生反馈信息的处理功能，包括提交反馈、查询反馈、处理反馈等
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Resource
    private FeedbackMapper feedbackMapper;
    
    @Resource
    private UserService userService;

    @Resource
    private StudentService studentService;

    /**
     * 提交反馈信息
     * 记录学生提交的反馈，设置初始状态为待处理
     *
     * @param feedback 反馈信息
     * @param request HTTP请求对象，用于获取当前登录用户
     * @return 新创建的反馈ID
     */
    @Override
    public Long submitFeedback(Feedback feedback, HttpServletRequest request) {
        // 获取当前登录用户
        User loginUser = userService.getLoginUser(request);
        Long studentId = loginUser.getStudentId();

        if(ObjectUtil.isNull(studentId)){
            throw new BusinessException(ErrorCode.ACCOUNT_DOES_NOT_EXIST);
        }
        Student student = studentService.getStudentById(studentId);
        if(ObjectUtil.isNull(student)){
            throw new BusinessException(ErrorCode.ACCOUNT_DOES_NOT_EXIST);
        }
        
        // 设置反馈信息
        feedback.setStudentId(student.getId());
        feedback.setStatus(0); // 设置初始状态为待处理
        
        feedbackMapper.insert(feedback);
        return feedback.getId();
    }

    /**
     * 获取反馈详情
     * 根据反馈ID查询反馈详细信息
     *
     * @param id 反馈ID
     * @return 反馈详情，不存在则返回null
     */
    @Override
    public Feedback getFeedbackById(Long id) {
        return feedbackMapper.selectById(id);
    }

    /**
     * 获取学生的反馈列表
     * 查询指定学生的所有反馈记录，支持按类型筛选
     *
     * @param studentId 学生ID
     * @param type 反馈类型（可选）
     * @return 反馈列表，按创建时间倒序排列
     */
    @Override
    public List<Feedback> getStudentFeedbacks(Long studentId, Integer type) {
        QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", studentId);
        if (type != null) {
            queryWrapper.eq("type", type);
        }
        queryWrapper.orderByDesc("create_time");
        return feedbackMapper.selectList(queryWrapper);
    }

    /**
     * 处理反馈
     * 更新反馈的处理状态、处理结果和处理人信息
     *
     * @param feedback 包含处理结果的反馈信息
     * @return 是否处理成功
     */
    @Override
    public boolean handleFeedback(Feedback feedback) {
        feedback.setUpdateTime(new Date());
        return feedbackMapper.updateById(feedback) > 0;
    }

    /**
     * 获取学生的反馈列表
     * @param request
     * @return
     */
    @Override
    public List<Feedback> getStudentFeedbacks(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Long studentId = loginUser.getStudentId();

        if(ObjectUtil.isNull(studentId)){
            throw new BusinessException(ErrorCode.ACCOUNT_DOES_NOT_EXIST);
        }
        Student student = studentService.getStudentById(studentId);
        if(ObjectUtil.isNull(student)){
            throw new BusinessException(ErrorCode.ACCOUNT_DOES_NOT_EXIST);
        }
        LambdaQueryWrapper<Feedback> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Feedback::getStudentId,student.getId());
        return list(queryWrapper);
    }
} 